<?php
class ImportPassCommand extends CConsoleCommand
{
	public function run($args)
	{
		if (!isset($args[0]))
		{
			echo "Not given file name!\n";
			return false;
		}
		$file=$args[0];

		if (!is_readable($file))
		{
			echo "Can't open file!\n";
			return false;
		}
		
		$fh = fopen($file, "r");

		$ln = 0;
		
		global $saveOldAttributes;
		$saveOldAttributes=true;
		
		while ($buffer = fgets($fh)) {
			++$ln;
			printf ("%2d: ", $ln);
			if ($buffer===FALSE) print ("FALSE\n");
			else {
				list($contract, $pass) = preg_split("/[\s,]+/", $buffer);
				$contract = substr($contract,2);
				printf ("%s - %s\n", $contract, $pass);
				
				$model=User::model()->find('t.contract=:contract',array(':contract'=>$contract));
				if ($model) {
					$model->password=$pass;
					$model->save();
				}
		
        		if ($buffer===FALSE) print ("FALSE\n");
			}
		}
		
		fclose($fh);
	}
}